package com.zl.tby.service.impl;

import com.zl.tby.core.base.bean.OptionType;
import com.zl.tby.core.base.bean.TbyPageResult;
import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.core.common.R;
import com.zl.tby.model.TbySpeciesCategory;
import com.zl.tby.model.TbySummarizeData;
import com.zl.tby.repository.TbySpeciesCategoryRepo;
import com.zl.tby.service.ITbyFileBusinessService;
import com.zl.tby.service.ITbySpeciesCategoryService;
import com.zl.tby.vo.TbySpeciesCategoryBean;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.data.annotation.Tran;
import org.noear.wood.DbContext;
import org.noear.wood.DbTableQuery;
import org.noear.wood.annotation.Db;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 山峰信息接口实现类
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:22
 */
@Component
public class TbySpeciesCategoryServiceImpl implements ITbySpeciesCategoryService {

    @Db
    private TbySpeciesCategoryRepo tbySpeciesCategoryRepo;
    @Db
    private DbContext db;

    @Override
    @Tran
    public R<TbySpeciesCategory> add(TbySpeciesCategory entity) {
        entity.setCreateBy(ToolsUtil.getLoginUserId());
        entity.setCreateTime(LocalDateTime.now());
        if (entity.getUseOverview() == null) {
            entity.setUseOverview(0);
        }
        long id = tbySpeciesCategoryRepo.insert(entity, true);
        entity.setId(id);
        return R.ok(entity);
    }

    @Override
    @Tran
    public R<TbySpeciesCategory> update(TbySpeciesCategory entity) {
        entity.setUpdateBy(ToolsUtil.getLoginUserId());
        entity.setUpdateTime(LocalDateTime.now());
        if (entity.getUseOverview() == null) {
            entity.setUseOverview(0);
        }
        tbySpeciesCategoryRepo.updateById(entity, false);
        return R.ok(entity);
    }

    @Override
    public R<Void> delete(Long id) {
        if (this.tbySpeciesCategoryRepo.selectCount(mp -> mp.whereTrue().andEq("parent_id", id)) > 0) {
            return R.failed("存在子分类无法删除");
        }
        // 验证分类是否被引用
        try {
            long count = db.table("tby_animal_plant").whereEq("type_level", id).selectCount();
            if (count > 0) {
                return R.failed("已被物种引用，无法删除");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return tbySpeciesCategoryRepo.deleteById(id) == 1 ? R.ok() : R.failed("删除失败");
    }

    @Override
    public TbySpeciesCategory get(Long id) {
        return tbySpeciesCategoryRepo.selectById(id);
    }

    @Override
    public TbyPageResult<TbySpeciesCategoryBean> treeTableList() {
        List<TbySpeciesCategoryBean> resultList = TbySpeciesCategoryBean.recurData(0L, this.tbySpeciesCategoryRepo.selectList(mp -> { mp.whereTrue(); }));
        return new TbyPageResult(resultList, new BigDecimal(resultList.size()).longValue(), 1, 1000);
    }

    @Override
    public List<TbySpeciesCategory> list() {
        return this.tbySpeciesCategoryRepo.selectList(mp -> { mp.whereTrue(); });
    }

    @Override
    public List<OptionType> options() {
        return TbySpeciesCategoryBean.recurRouteOptions(0L, this.tbySpeciesCategoryRepo.selectList(mp -> { mp.whereTrue(); }));
    }

    @Override
    public List<Long> getUseOverviewIdList() {
        List<TbySpeciesCategory> dbList = this.tbySpeciesCategoryRepo.selectList(mp -> {
           mp.whereEq("use_overview", 1);
        });
        return dbList.stream().map(item -> item.getId() ).collect(Collectors.toList());
    }

    @Override
    public Map<Long, String> mapData() {
        List<TbySpeciesCategory> dbList = this.list();
        Map<Long, String> data = new HashMap<>(0);
        if (dbList != null) {
            for (TbySpeciesCategory entity : dbList) {
                data.put(entity.getId(), entity.getCategoryName());
            }
        }
        return data;
    }
}
